package org.dromara.nursing.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.nursing.domain.vo.FoodServiceVo;
import org.dromara.nursing.domain.bo.FoodServiceBo;
import org.dromara.nursing.service.IFoodServiceService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 饮食服务
 *
 * @author Xue
 * @date 2025-04-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/nursing/foodService")
public class FoodServiceController extends BaseController {

    private final IFoodServiceService foodServiceService;

    /**
     * 查询饮食服务列表
     */
    @SaCheckPermission("nursing:foodService:list")
    @GetMapping("/list")
    public TableDataInfo<FoodServiceVo> list(FoodServiceBo bo, PageQuery pageQuery) {
        return foodServiceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出饮食服务列表
     */
    @SaCheckPermission("nursing:foodService:export")
    @Log(title = "饮食服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FoodServiceBo bo, HttpServletResponse response) {
        List<FoodServiceVo> list = foodServiceService.queryList(bo);
        ExcelUtil.exportExcel(list, "饮食服务", FoodServiceVo.class, response);
    }

    /**
     * 获取饮食服务详细信息
     *
     * @param serviceId 主键
     */
    @SaCheckPermission("nursing:foodService:query")
    @GetMapping("/{serviceId}")
    public R<FoodServiceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long serviceId) {
        return R.ok(foodServiceService.queryById(serviceId));
    }

    /**
     * 新增饮食服务
     */
    @SaCheckPermission("nursing:foodService:edit")
    @Log(title = "饮食服务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<FoodServiceVo> add(@Validated(AddGroup.class) @RequestBody FoodServiceBo bo) {
        FoodServiceVo result = foodServiceService.insertByBo(bo);
        if (result != null) {
            return R.ok(result);
        } else {
            return R.fail("新增饮食服务失败");
        }
    }

    /**
     * 修改饮食服务
     */
    @SaCheckPermission("nursing:foodService:edit")
    @Log(title = "饮食服务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FoodServiceBo bo) {
        return toAjax(foodServiceService.updateByBo(bo));
    }

    /**
     * 删除饮食服务
     *
     * @param serviceIds 主键串
     */
    @SaCheckPermission("nursing:foodService:remove")
    @Log(title = "饮食服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{serviceIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] serviceIds) {
        return toAjax(foodServiceService.deleteWithValidByIds(List.of(serviceIds), true));
    }
}
